//
//  NSCustomTipView.m
//  MacDevTest
//
//  Created by lbxia on 2022/9/13.
//  Copyright © 2022 com.lbx. All rights reserved.
//

#import "NSCustomTipView.h"
//#import <CoreImage/CIFilter.h>

@interface NSCustomTipView()
@property (nonatomic, assign) NSTimer *timerProgress;

@property (nonatomic, strong) NSLayoutConstraint *widthConstraint;
@property (nonatomic, strong) NSView *progressView;

@property (nonatomic, assign) NSCustomTipType tipType;

//百分比
@property (nonatomic, assign) double progress;
@end

@implementation NSCustomTipView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (double)getCustomProgress {
    return _progress;
}

- (void)setCustomProgress:(double)progress {
    
    if (![NSThread isMainThread]) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self setCustomProgress:progress];
        });
        return;
    }
    
    self.progress = progress;
    
    [self.progressView removeConstraint:self.widthConstraint];
    self.widthConstraint = [self.progressView.widthAnchor constraintEqualToConstant:self.frame.size.width * self.progress];
    self.widthConstraint.active = YES;
}


- (void)startProgress{
    
    if (_timerProgress) {
        return;
    }
    
    self.progress = 0.0;
    
    
    _timerProgress = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(refreshProgress) userInfo:nil repeats:YES];
}

- (void)refreshProgress {

    self.progress += 0.5;
    if (self.progress > 80) {
        self.progress = 80;
    }
    
    [self.progressView removeConstraint:self.widthConstraint];
    self.widthConstraint = [self.progressView.widthAnchor constraintEqualToConstant:self.frame.size.width * self.progress / 100.];
    self.widthConstraint.active = YES;
}

- (void)refreshFinish {
    
    [self stopProgress];
    
    [self.progressView removeConstraint:self.widthConstraint];
    self.widthConstraint = [self.progressView.widthAnchor constraintEqualToConstant:self.frame.size.width];
    self.widthConstraint.active = YES;
}

- (void)stopProgress {
    
    if (_timerProgress) {
        [_timerProgress invalidate];
        self.timerProgress = nil;
    }
}


/// 显示信息，默认2s后消失,progress类型除外，需要自行调用消失接口
/// @param tipType tipType description
/// @param pos pos description
/// @param content content description
+ (NSCustomTipView*)showWithTipType:(NSCustomTipType)tipType pos:(NSCustomTipPostion)pos content:(NSString*)content {
    
    CGFloat diff = 40;
    if (pos == NSCustomTipPostion_Center) {
        diff = 0;
    }
    return [self showWithTipType:tipType pos:pos posDiff:diff content:content afterSeconds:2];
}




/// 显示信息
/// @param tipType 信息类型
/// @param pos 信息显示未知
/// @param posDiff 位置差
/// @param content 信息内容
/// @param afterSeconds 几秒后显示，如果是进度条类型，该参数无效，需要自行调用接口
+ (NSCustomTipView*)showWithTipType:(NSCustomTipType)tipType
                                pos:(NSCustomTipPostion)pos
                            posDiff:(CGFloat)posDiff
                            content:(NSString*)content
                       afterSeconds:(NSInteger)afterSeconds {
    
    if (![NSThread isMainThread]) {
        
        __block NSCustomTipView *tipView = nil;

        dispatch_sync(dispatch_get_main_queue(), ^{
            
            tipView = [self showWithTipType:tipType pos:pos content:content];
        });
        
        return tipView;
    }
    
    NSView *superView = [NSApplication sharedApplication].keyWindow.contentView;
    
    //透明层


    //内容背景View
    NSCustomTipView *backView = [[NSCustomTipView alloc]init];
    backView.wantsLayer = YES;
    backView.layer.backgroundColor = NSColor.whiteColor.CGColor;
    backView.layer.cornerRadius = 6;
    [superView addSubview:backView];
    backView.translatesAutoresizingMaskIntoConstraints = NO;
    [backView.centerXAnchor constraintEqualToAnchor:superView.centerXAnchor].active = YES;
    
    
    switch (pos) {
        case NSCustomTipPostion_Top:
            [backView.topAnchor constraintEqualToAnchor:superView.topAnchor constant:posDiff].active = YES;

            break;
        case NSCustomTipPostion_Center:
            [backView.centerYAnchor constraintEqualToAnchor:superView.centerYAnchor constant:posDiff].active = YES;
            break;
        case NSCustomTipPostion_Bottom:
        case NSCustomTipPostion_None:
            [backView.bottomAnchor constraintEqualToAnchor:superView.bottomAnchor constant:-posDiff].active = YES;
            break;
            
        default:
            break;
    }
    
    
    backView.tipType = tipType;
    
    //阴影
    backView.shadow = [[NSShadow alloc]init];
    backView.layer.shadowOffset = NSMakeSize(0, -2);
    backView.layer.shadowOpacity = 0.6;
    backView.layer.shadowColor = [NSColor colorWithRed:0 green:0 blue:0 alpha:0.25].CGColor;
    backView.layer.shadowRadius = 4;
    
    //左侧图标
    NSImageView *leftImageView = [[NSImageView alloc]init];
    
    NSString *iconName = nil;
    switch (tipType) {
        case NSCustomTipType_Success:
            iconName = @"icon_success";
            break;
        case NSCustomTipType_Fail:
            iconName = @"icon_error";
            break;
        case NSCustomTipType_Progress:
        case NSCustomTipType_CustomProgress:
            iconName = @"icon_dot";
            break;
            
        default:
            break;
    }
    
    if (iconName) {
        leftImageView.image = [NSImage imageNamed:iconName];
    }
    
    leftImageView.wantsLayer = YES;
//    leftImageView.layer.backgroundColor = NSColor.redColor.CGColor;
    [backView addSubview:leftImageView];
    leftImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [leftImageView.leadingAnchor constraintEqualToAnchor:backView.leadingAnchor constant:16].active = YES;
    [leftImageView.topAnchor constraintEqualToAnchor:backView.topAnchor constant:13].active = YES;
    [leftImageView.bottomAnchor constraintEqualToAnchor:backView.bottomAnchor constant:-13].active = YES;
    [leftImageView.widthAnchor constraintEqualToConstant:14].active = YES;
    [leftImageView.heightAnchor constraintEqualToConstant:14].active = YES;
    
    //右边内容
    NSTextField *labelContent = [[NSTextField alloc]init];
    labelContent.wantsLayer = YES;
    labelContent.backgroundColor = NSColor.clearColor;
    labelContent.bordered = NO;
    labelContent.selectable = NO;
    labelContent.editable  = NO;
    labelContent.font = [NSFont systemFontOfSize:14];
    labelContent.maximumNumberOfLines = 1;
    //92,98,107
    labelContent.textColor = [NSColor colorWithRed:92/255. green:98/255. blue:107/255. alpha:1];
    labelContent.stringValue = content;
    [backView addSubview:labelContent];
    
    [labelContent sizeToFit];
    
    labelContent.translatesAutoresizingMaskIntoConstraints = NO;
    [labelContent.centerYAnchor constraintEqualToAnchor:leftImageView.centerYAnchor].active = YES;
    [labelContent.leadingAnchor constraintEqualToAnchor:leftImageView.trailingAnchor constant:8].active = YES;
    [labelContent.trailingAnchor constraintEqualToAnchor:backView.trailingAnchor constant:-16].active = YES;
    [labelContent.widthAnchor constraintEqualToConstant:labelContent.frame.size.width + 4].active = YES;
    
    
    if (tipType == NSCustomTipType_Progress || tipType == NSCustomTipType_CustomProgress) {
        
        NSColor *progressColor = [NSColor colorWithRed:0 green:120/255. blue:250/255. alpha:1];
        
        //进度条
        NSView *progressView = [[NSView alloc]init];
        progressView.wantsLayer = YES;
        progressView.layer.backgroundColor = progressColor.CGColor;
        progressView.layer.cornerRadius = 1;
        [backView addSubview:progressView];
        progressView.translatesAutoresizingMaskIntoConstraints = NO;
        [progressView.leadingAnchor constraintEqualToAnchor:backView.leadingAnchor constant:1].active = YES;
        [progressView.bottomAnchor constraintEqualToAnchor:backView.bottomAnchor].active = YES;
        [progressView.heightAnchor constraintEqualToConstant:2].active = YES;
        NSLayoutConstraint *widthConstraint = [progressView.widthAnchor constraintEqualToConstant:20];
        widthConstraint.active = YES;
        
        backView.widthConstraint = widthConstraint;
        backView.progressView = progressView;
        
        if (tipType == NSCustomTipType_Progress) {
            
            [backView startProgress];
        }
        
       
    }else{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(afterSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            [backView disAppear];
        });
    }

    return backView;
}


- (void)disAppear {
    
    if (![NSThread isMainThread]) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self disAppear];
        });
        return;
    }
    
    if (_tipType == NSCustomTipType_Progress) {
        
        [self refreshFinish];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            [self removeFromSuperview];
        });
    }else
    
    [self removeFromSuperview];
}
@end
